/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Ventanas;

import Gestores.Entidades.EntidadCuota;
import Gestores.Entidades.EntidadPagoMatricula;
import Negocios.CuotaNegocio;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.Date;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Lion
 */
public class BuscarCuota extends javax.swing.JFrame {

    /**
     * Creates new form BuscarCuota
     */
    private CuotaNegocio negocio;
    private DefaultTableModel model;
    private  String campos[]= {"Mes", "Anho"};
    private GestionarCuota ventanareferencia;
    
    public BuscarCuota() {
        initComponents();
        negocio = new CuotaNegocio();
      
        model = new DefaultTableModel(campos, 0);
        this.table_datos.setModel(model);
        setTitle("Colegio Economista-Busqueda Cuota");
        
        this.table_datos.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                int row = table_datos.getSelectedRow();
                int col = table_datos.getSelectedColumn();
                  
                       EntidadCuota cu= new EntidadCuota();
                       
                       cu.setMes(negocio.obtenerMesCodigo((String) table_datos.getValueAt(row, 0)));
                       cu.setAnho((Integer)table_datos.getValueAt(row, 1));
                      
                      ventanareferencia.cargarDatos(cu);
                      BuscarCuota.this.dispose();
        }});
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        chb_mes = new javax.swing.JCheckBox();
        chb_Anho = new javax.swing.JCheckBox();
        cmb_mes = new com.toedter.calendar.JMonthChooser();
        cmb_year = new com.toedter.calendar.JYearChooser();
        jButton1 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        table_datos = new javax.swing.JTable();
        jScrollPane2 = new javax.swing.JScrollPane();
        txt_mensaje = new javax.swing.JTextArea();
        jLabel2 = new javax.swing.JLabel();

        jLabel1.setText("jLabel1");

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Criterio de Busqueda"));

        chb_mes.setText("Buscar Por Mes");

        chb_Anho.setText("Buscar Por Anho");
        chb_Anho.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chb_AnhoActionPerformed(evt);
            }
        });

        jButton1.setText("Buscar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(4, 4, 4)
                        .addComponent(chb_Anho))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(chb_mes)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(cmb_mes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(cmb_year, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jButton1)))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cmb_mes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cmb_year, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1)
                .addGap(0, 0, Short.MAX_VALUE))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(chb_Anho)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(chb_mes)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        table_datos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(table_datos);

        txt_mensaje.setColumns(20);
        txt_mensaje.setRows(5);
        jScrollPane2.setViewportView(txt_mensaje);

        jLabel2.setText("Mensaje");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 392, Short.MAX_VALUE)
                        .addComponent(jScrollPane2))
                    .addComponent(jLabel2))
                .addContainerGap(23, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 17, Short.MAX_VALUE)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(23, 23, 23))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void chb_AnhoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chb_AnhoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_chb_AnhoActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        if (!this.chb_Anho.isSelected() && this.chb_mes.isSelected()) {
            try {
                ArrayList<Object> l = negocio.buscarCuotaPorMes(this.cmb_mes.getMonth());
                if (l != null && l.size() > 0) {
                    model = negocio.tabularDatos(l);
                    this.table_datos.setModel(model);
                } else {
                    this.table_datos.setModel(model);
                    this.txt_mensaje.setText("Error,La cuota no existe");
                }
            } catch (Exception ex) {
                this.table_datos.setModel(model);
                this.txt_mensaje.setText("Error en la busqueda verifique mes");
                Logger.getLogger(BuscarCuota.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        if (this.chb_Anho.isSelected() && !this.chb_mes.isSelected()) {
             try {
                ArrayList<Object> l = negocio.buscarCuotaPorAnho(this.cmb_year.getYear());
                if (l != null && l.size() > 0) {
                    model = negocio.tabularDatos(l);
                    this.table_datos.setModel(model);
                } else {
                    this.table_datos.setModel(model);
                    this.txt_mensaje.setText("Error,La cuota no existe");
                }
            } catch (Exception ex) {
                this.table_datos.setModel(model);
                this.txt_mensaje.setText("Error en la busqueda verifique anho");
                Logger.getLogger(BuscarCuota.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        if (this.chb_Anho.isSelected() && this.chb_mes.isSelected()) {
             try {
                ArrayList<Object> l = negocio.buscarCuotaPorMesAnho(this.cmb_mes.getMonth(),this.cmb_year.getYear());
                if (l != null && l.size() > 0) {
                    model = negocio.tabularDatos(l);
                    this.table_datos.setModel(model);
                } else {
                    this.table_datos.setModel(model);
                    this.txt_mensaje.setText("Error,La cuota no existe");
                }
            } catch (Exception ex) {
                this.table_datos.setModel(model);
                this.txt_mensaje.setText("Error en la busqueda");
                Logger.getLogger(BuscarCuota.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        if (!this.chb_Anho.isSelected() && !this.chb_mes.isSelected())
            this.txt_mensaje.setText("Debe seleccionar un criterio de busqueda");
    }//GEN-LAST:event_jButton1ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(BuscarCuota.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(BuscarCuota.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(BuscarCuota.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(BuscarCuota.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new BuscarCuota().setVisible(true);
            }
        });
    }
     public void enlazar(GestionarCuota pc){
            this.ventanareferencia = pc;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JCheckBox chb_Anho;
    private javax.swing.JCheckBox chb_mes;
    private com.toedter.calendar.JMonthChooser cmb_mes;
    private com.toedter.calendar.JYearChooser cmb_year;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable table_datos;
    private javax.swing.JTextArea txt_mensaje;
    // End of variables declaration//GEN-END:variables
}
